<?php 
class Encapsulable
{	
	private $properties = array();
	
	public function __construct(){}
	
	public function __set($key, $value)
	{
		$method = 'set' . ucfirst($key);
		if(method_exists($this, $method))
		{
			$this->$method($value);
		}
		else
		{
			throw new Exception("Property <b>$key</b> is not defined");
		}
	}
	
	public function __get($key)
	{
		$method = 'get' . ucfirst($key);
		if(method_exists($this, $method))
		{
			return $this->$method();
		}
		else
		{
			throw new Exception("Property <b>$key</b> is not defined");
		}
	}
	
	public function __isset($key)
	{
		$method = 'get' . ucfirst($key);
		return method_exists($this, $method);
	}
	
	public function toArray()
	{
		$properties = array();
		foreach(get_class_methods($this) as $method)
		{
			if(preg_match('#^get([\w]+)$#',$method,$matches))
			{
				$key = $matches[1];
				$value = $this->$method();
				$properties[$key] = $value;
			}
		}
		return $properties;
	}
}